#include "LKH.h"
#include "Genetic.h"
#include <stdio.h>
#include <math.h>
#include <io.h>
/*
 * This file contains the main function of the program.
 */
int Execute()
{
	GainType Cost;
	double Time, LastTime = GetTime();


	double multiplier = 1;
// 	/* Read the specification of the problem */
// 	if (argc >= 2)
// 		ParameterFileName = argv[1];



	ReadParameters();
	MaxMatrixDimension = 10000;
	ReadProblem();

   	printf("addk:");
   	scanf("%lf",&multiplier);
   	printf("maxloop:");
   	scanf("%d",&MaxLoop);
//	multiplier =0.2;

	if(OutputTourFileName==NULL)
	{
		OutputTourFileName=(char *)malloc(sizeof(char)*100);
	}
	sprintf(OutputTourFileName,"%s.txt",ProblemFileName);
	OutputFile = fopen(OutputTourFileName, "w");

	if (SubproblemSize > 0) {
		if (DelaunayPartitioning)
			SolveDelaunaySubproblems();
		else if (KarpPartitioning)
			SolveKarpSubproblems();
		else if (KMeansPartitioning)
			SolveKMeansSubproblems();
		else if (RohePartitioning)
			SolveRoheSubproblems();
		else if (MoorePartitioning || SierpinskiPartitioning)
			SolveSFCSubproblems();
		else
			SolveTourSegmentSubproblems();
		return EXIT_SUCCESS;
	}
	AllocateStructures();
	CreateCandidateSet();
	InitializeStatistics();

	if (Norm != 0)
		BestCost = PLUS_INFINITY;
	else {
		/* The ascent has solved the problem! */
		Optimum = BestCost = (GainType) LowerBound;
		UpdateStatistics(Optimum, GetTime() - LastTime);
		RecordBetterTour();
		RecordBestTour();
		WriteTour(OutputTourFileName, BestTour, BestCost);
		WriteTour(TourFileName, BestTour, BestCost);
		Runs = 0;
	}

//  	if(Dimension > 150)
//  	{
//  		ADDK = ChooseADDK();
//   		printff("addk = %f\n",ADDK);
//  		ADDK = sqrt(ADDK);
//  	}
//  	else
// 		ADDK = Dimension/Precision;
	ADDK = LowerBound / Dimension;
	ADDK *= multiplier;
//	ADDK *= 10;
	fprintf(OutputFile,"addk = %f\n",ADDK);

	/* Find a specified number (Runs) of local optima */
	for (Run = 1; Run <= Runs; Run++) {
		LastTime = GetTime();

		Cost = FindTour();      /* using the Lin-Kernighan heuristic */
		if (Run > 1) {
			Cost = MergeBetterTourWithBestTour();
			if (Cost < BestCost)
				RecordBetterTour();
		}
		if (Cost < BestCost) {
			BestCost = Cost;
			RecordBestTour();
// 			if (MaxTrials == 0)
// 				WriteTour(OutputTourFileName, BestTour, BestCost);
// 			WriteTour(TourFileName, BestTour, BestCost);
		}
		if (Cost < Optimum) {
			if (FirstNode->InputSuc) {
				Node *N = FirstNode;
				while ((N = N->InputSuc = N->Suc) != FirstNode);
			}
			Optimum = Cost;
			printff("*** New optimum = " GainFormat " ***\n\n", Optimum);
		}
		Time = fabs(GetTime() - LastTime);
		UpdateStatistics(Cost, Time);
		if (TraceLevel >= 1 && Cost != PLUS_INFINITY) {
			printff("Run %d: Cost = " GainFormat, Run, Cost);
			fprintf(OutputFile,"Run %d:Cost = "GainFormat, Run, Cost);
			if (Optimum != MINUS_INFINITY && Optimum != 0){
				printff(", Gap = %0.3f%%",
					100.0 * (Cost - Optimum) / Optimum);
				fprintf(OutputFile,", Gap = %0.3f%%", 100.0 * (Cost - Optimum) / Optimum);
			}
			printff(", Time = %0.1f sec. %s\n\n", Time,
				Cost < Optimum ? "<" : Cost == Optimum ? "=" : "");
			fprintf(OutputFile, ", Time = %0.1f sec. %s\n\n", Time,
				Cost < Optimum ? "<" : Cost == Optimum ? "=" : "");
		}
		SRandom(++Seed);
	}
	PrintStatistics();
	return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
	struct _finddata_t finddata;
	int done,handle;
	done=1;
	printf("Directory listing of *.*\n");
	handle = _findfirst("par\\*.par",&finddata);
	if(handle==-1)
		return 0;
	do 
	{
		ParameterFileName=finddata.name;
		Execute();
		FirstGain23=1;

	} while (!(done=_findnext(handle,&finddata)));
    system("pause");
    return EXIT_SUCCESS;
}
